﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;

namespace Possan.Localization
{
	public class Translate
	{
		static ILibraryProvider lib = null;
		static bool _debug = false;

		/// <summary>
		/// Enable debug information on outputted strings
		/// </summary>
		public static bool Debugging
		{
			get
			{
				return _debug;
			}
			set
			{
				_debug = value;
			}
		}

		public static ILibraryProvider Library
		{
			get
			{
			//	if (lib == null)
			//		lib = new Library();
				return lib;
			}
			set
			{
				lib = value;
			}
		}
		/*
		public static Library CurrentLibrary
		{
			get
			{
				if (lib == null)
					lib = new Library();

				return lib;
			}
		}
		*/

		public static string Phrase(object callee, string defaultstring)
		{
			return Phrase(callee.GetType(), defaultstring, Thread.CurrentThread.CurrentUICulture);
		}

		public static string Phrase(object callee, string defaultstring, CultureInfo culture)
		{
			return Phrase(callee.GetType(), defaultstring, culture);
		}

		public static string Phrase(Type callee, string defaultstring)
		{
			return Phrase(callee, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}


		public static string Phrase(Type callee, string defaultstring, CultureInfo culture)
		{
			return Phrase(callee.FullName, defaultstring, culture);
		}

		public static string Phrase(string namespacename, string defaultstring)
		{
			return Phrase(namespacename, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}

		public static string Phrase(string namespacename, string defaultstring, CultureInfo culture)
		{
			string ret = "";
			if (lib != null)
			{
				ret = lib.TranslatePhrase(namespacename.ToLower(), defaultstring, culture);
				if (_debug)
					ret = "[[culture=" + culture + ", namespace=" + namespacename + ", orig=" + defaultstring.Substring(0, Math.Min(20, defaultstring.Length)) + ", translated=" + ret + "]]";
			}
			else
			{
				ret = defaultstring;
				if (_debug)
					ret = "[[lib=null, culture=" + culture + ", namespace=" + namespacename + ", orig=" + defaultstring.Substring(0, Math.Min(20, defaultstring.Length)) + ", translated=" + ret + "]]";
			}
			return ret;

		}







		public static string ID(object callee, string id, string defaultstring)
		{
			return ID(callee.GetType().FullName, id, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}

		public static string ID(object callee, string id, string defaultstring, CultureInfo culture)
		{
			return ID(callee.GetType().FullName, id, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}

		public static string ID(Type callee, string id, string defaultstring, CultureInfo culture )
		{
			return ID(callee.FullName, id, defaultstring, culture);
		}

		public static string ID(Type callee, string id, string defaultstring)
		{
			return ID(callee.FullName, id, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}
		 
		public static string ID(string namespacename, string id, string defaultstring)
		{
			return ID(namespacename, id, defaultstring, Thread.CurrentThread.CurrentUICulture);
		}
		
		public static string ID(string namespacename, string id, string defaultstring, CultureInfo culture)
		{
			string ret = "";
			if (lib != null)
			{
				ret = lib.TranslatePhraseByID(namespacename.ToLower(), id, defaultstring, culture);
				if (_debug)
					ret = "[[culture=" + culture + ", namespace=" + namespacename + ", id=" + id + ", orig=" + defaultstring.Substring(0, Math.Min(20, defaultstring.Length)) + ", translated=" + ret + "]]";
			}
			else
			{
				ret = defaultstring;
				if (_debug)
					ret = "[[lib=null, culture=" + culture + ", namespace=" + namespacename + ", id=" + id + ", orig=" + defaultstring.Substring(0, Math.Min(20, defaultstring.Length)) + ", translated=" + ret + "]]";
			}
			return ret;

		}


		/*
		public static void Initialize()
		{
			// lib.Clear();
			CurrentLibrary.Clear();
		}

		public static void Load(string p)
		{
			CurrentLibrary.ImportXML(p);
		}
		*/
	}
}
